package Options::UserSync::API::UIUserDataUpdate;

use strict;
use warnings;
use DE_EPAGES::Core::API::Error qw (Error );
use DE_EPAGES::Trigger::API::Trigger qw ( TriggerHook );
use DE_EPAGES::Core::API::Log qw (LogDebug GetLog);
use DE_EPAGES::Object::API::Factory
    qw( LoadObject ExistsObjectByPath LoadObjectByPath Transaction DeleteObject );
use DE_EPAGES::Order::API::LineItemContainer qw(SaveShipping);
use DE_EPAGES::Object::API::Object::Object qw(clone insertChild);
use Data::Dumper;

use Options::Attributes::API::InterfaceCustomerGet;
use Options::UserSync::API::AddressFieldsConverter;

sub UIAfterLoginUser {
    my ($hParams)      = @_;
    my $Servlet        = $hParams->{'Servlet'};
    my $User           = $Servlet->user;
    my $CustomersArray = $User->get('Customers');
    my @AddressesArray;
    my $UsersAddressesArray = $User->get('Addresses');
    GetLog->debug("UIAfterLoginUser - TRIGERED for user: " . $User->get('ID'));
    #Making sure that the users are not like developer or admin
    if (   defined $User
        && $User->get('Site')->get('Alias') ne "System"
        && scalar(@$CustomersArray) >= 1 )
    {

        #Bibliophile we will have always customer user relation 1:1
        my $ShopperID = $CustomersArray->[0]->get('OptionsShopperID');
        my $CustomerNumber = $CustomersArray->[0]->get('OptionsCustId');
        if ($CustomerNumber eq '') {$CustomerNumber = 0;}
        if ( $ShopperID eq '' ) {
            GetLog->debug(
                "AfterLoginUser - The user hasn't proper ShopperID - cant be synchronized. "
                    . $User->get('ID') );
        }
        else {
            my %UserSearchRequest = (
#                'ShopperID'       => $ShopperID,
#                'GetAllCustomers' => 'false',
                'CustomerNumber'        => $CustomerNumber,
                'ShopperID'             => $ShopperID,
                'GetAllCustomers'       => 'false',
            );

             GetLog->debug("AfterLoginUser - Search request: " . Dumper(\%UserSearchRequest) );

            my $WSLinkGet
                = new Options::Attributes::API::InterfaceCustomerGet(
                "CustomerSearch", "CustomerSearchRequest",
                \%UserSearchRequest, "Customer" );
            my $Customer       = $WSLinkGet->hitMe();
             GetLog->debug("AfterLoginUser - Customer $ShopperID from OPTIONS: " . Dumper($Customer) );
         
         if (ref($Customer) eq "HASH") {
            GetLog->debug("AfterLoginUser - Customer $ShopperID Is HASH go WITH update");          
            my $BillingAddress = $CustomersArray->[0]->get('BillingAddress');
            my $UserBillingAddress = $User->get('BillingAddress');
            my $FieldsConverter
                = new Options::UserSync::API::AddressFieldsConverter();
            $FieldsConverter->convertFromOptionsBillingAddress(
                $BillingAddress, $Customer );
            $FieldsConverter->convertFromOptionsBillingAddress(
                $UserBillingAddress, $Customer );

            
	    if($CustomerNumber == 0) {
                my $CustomerRecord = $CustomersArray->[0];
                $CustomerRecord->set( {'OptionsCustId' => $Customer->{'CustomerNumber'}} );                     
            }


            #GetLog->error("  \n" . $CustomersArray->[0]->get('ObjectID'));
            
####          
           if($Customer->{AdditionalAddresses} eq "") {
                 
           
           } else {
            if (ref($Customer->{AdditionalAddresses}
                        ->{CustomerAdditionalAddress}
                ) eq "HASH"
                )
            {
                push( @AddressesArray,
                    $Customer->{AdditionalAddresses}
                        ->{CustomerAdditionalAddress} );
                $FieldsConverter->convertFromOptionsShippingAddress(
                    $UsersAddressesArray, \@AddressesArray, $User, 39830);

                #We have just One address
            }
            elsif (
                ref($Customer->{AdditionalAddresses}
                        ->{CustomerAdditionalAddress}
                ) eq "ARRAY"
                )
            {
                $FieldsConverter->convertFromOptionsShippingAddress(
                    $UsersAddressesArray,
                    $Customer->{AdditionalAddresses}
                        ->{CustomerAdditionalAddress},
                    $User, 39830
                );

                #We have more than one addres
            }
           }
            
         } else {
            GetLog->debug("AfterLoginUser - Customer $ShopperID No respond from OPTIONS - NO UPDATE");               
         }
        }
    }

}

1;
